package io.jpress.web.directive;

import com.jfinal.template.Env;
import com.jfinal.template.io.Writer;
import com.jfinal.template.stat.Scope;
import io.jboot.utils.StrUtil;
import io.jboot.web.directive.annotation.JFinalDirective;
import io.jboot.web.directive.base.JbootDirectiveBase;
import io.jpress.JPressOptions;

@JFinalDirective("staticize")
public class StaticizeDirective extends JbootDirectiveBase {

    @Override
    public void onRender(Env env, Scope scope, Writer writer) {
        boolean staticizeEnable = JPressOptions.getAsBool("staticize_enable", false);
        if (!staticizeEnable) {
            return;
        }

        String path = getParaToString(0, scope);
        if (StrUtil.isBlank(path)) {
            return;
        }

        String a = " <a open-type=\"confirm\" data-title=\"确定要生成静态页面吗？\" href=\"/admin/staticize/doGen?path=" + path + "\">静态生成</a>";

        String separator = getParaToString(1, scope);
        if (StrUtil.isNotBlank(separator)) {
            a = separator + a;
        }

        renderText(writer, a);
    }
}
